<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    canvas {
      border: 1px solid #ccc;
    }
  </style>
</head>
<body>
  <canvas class="canvas" width="500" height="500"></canvas>
  <!-- 顶点着色器 -->
  <script id="vertex-shader" type="x-shader/x-vertex">
    attribute vec4 aPos;
    void main() {
      float radian = radians(10.0);
      float cos = cos(radian);
      float sin = sin(radian);
      mat4 mx = mat4(
        1, 0, 0, 0,
        0, cos, -sin, 0,
        0, sin, cos, 0,
        0, 0, 0, 1
      );
      mat4 my = mat4(
        cos, 0, -sin, 0,
        0, 1, 0, 0,
        sin, 0, cos, 0,
        0, 0, 0, 1
      );
      gl_Position = mx * my * aPos;
      gl_PointSize = 10.0;
    }
  </script>
  <!-- 片段着色器 -->
  <script id="fragment-shader" type="x-shader/x-fragment">
    void main() {
      gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
    } 
  </script>
  <script type="module">
    import initShader from "./utils/initShader.js";
    const canvas = document.querySelector(".canvas");
    const gl = canvas.getContext("webgl");
    const program = initShader(gl);

    /*
      [
        0.5, 0.5, 0.5,
        -0.5, 0.5, 0.5,
        -0.5, -0.5, 0.5,
        0.5, -0.5, 0.5,

        0.5, 0.5, -0.5,
        -0.5, 0.5, -0.5,
        -0.5, -0.5, -0.5,
        0.5, -0.5, -0.5

        0.5, 0.5, 0.5,
        0.5, 0.5, -0.5,

        -0.5, 0.5, 0.5,
        -0.5, 0.5, -0.5,

        -0.5, -0.5, 0.5,
        -0.5, -0.5, -0.5,

        0.5, -0.5, 0.5,
        0.5, -0.5, -0.5
      ]
    */
    const vertices = new Float32Array([
      0.5, 0.5, 0.5,
      -0.5, 0.5, 0.5,
      -0.5, -0.5, 0.5,
      0.5, -0.5, 0.5,
      0.5, 0.5, -0.5,
      -0.5, 0.5, -0.5,
      -0.5, -0.5, -0.5,
      0.5, -0.5, -0.5,

      0.5, 0.5, 0.5,
      0.5, 0.5, -0.5,

      -0.5, 0.5, 0.5,
      -0.5, 0.5, -0.5,

      -0.5, -0.5, 0.5,
      -0.5, -0.5, -0.5,

      0.5, -0.5, 0.5,
      0.5, -0.5, -0.5,
    ])

    // 缓冲区
    const buffer = gl.createBuffer();
    gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
    gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);

    const posLocation = gl.getAttribLocation(program, "aPos");

    gl.vertexAttribPointer(
      posLocation,
      3,
      gl.FLOAT,
      false,
      3 * Float32Array.BYTES_PER_ELEMENT,
      0
    )

    gl.enableVertexAttribArray(posLocation);

    gl.drawArrays(gl.POINTS, 0, 4);
    gl.drawArrays(gl.LINE_LOOP, 0, 4);
    gl.drawArrays(gl.POINTS, 4, 4);
    gl.drawArrays(gl.LINE_LOOP, 4, 4);
    // 点与线绘制缕空立方体，也可以用三角形来绘制不缕空的立方体
    gl.drawArrays(gl.LINES, 8, 8);
  </script>
</body>
</html>